package jp.seraph.jsmf.motion;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.seraph.jsade.task.AgentTask;
import jp.seraph.jsmf.message.MessageUtil;

public class DefaultMotionSession implements EditableMotionSession {
    public DefaultMotionSession(String aName) {

        mName = aName;
        mElementMap = new HashMap<String, MotionElement>();
    }

    public DefaultMotionSession(String aName, MotionElement... aElements){
        this(aName);

        for (MotionElement tElement : aElements) {
            this.addElement(tElement);
        }
    }

    private Map<String, MotionElement> mElementMap;
    private String mName;

    public String getName() {
        return mName;
    }

    /**
     *
     * @see jp.seraph.jsmf.motion.MotionSession#getElement(java.lang.String)
     */
    public MotionElement getElement(String aElementName) {
        return mElementMap.get(aElementName);
    }

    public void addElement(MotionElement aElement) {
        String tName = aElement.getName();
        if(mElementMap.containsKey(tName))
            throw new RuntimeException(MessageUtil.duplicateElementName(tName));
        mElementMap.put(tName, aElement);
    }

    public boolean removeElement(MotionElement aElement) {
        return mElementMap.remove(aElement.getName()) != null;
    }

    public int getElementCount() {
        return mElementMap.size();
    }

    /**
     *
     * @see jp.seraph.jsmf.motion.MotionSession#toElementList()
     */
    public List<MotionElement> toElementList() {
        return new ArrayList<MotionElement>(mElementMap.values());
    }

    /**
     *
     * @see jp.seraph.jsmf.motion.MotionSession#toTask()
     */
    public AgentTask toTask() {
        return new SessionTask(this);
    }
}
